package com.dd.lnetwork

import android.content.Context
import com.dd.lnetwork.provider.IProvider
import com.google.gson.ExclusionStrategy
import com.google.gson.FieldAttributes
import com.google.gson.GsonBuilder
import io.reactivex.rxjava3.schedulers.Schedulers
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit


class RetrofitClient : IProvider {
    private lateinit var baseOkClient: OkHttpClient
    private var _deviceId: String = "63ee30f9702f44f5922174ecdcd0069f"
    private var _token: String? = null
    private var _cookies: HashSet<String>? = null

    constructor(

    ) {
    }

    constructor(
        deviceId: String,
        token: String?,
        cookies: HashSet<String>?
    ) {
        _deviceId = deviceId
        _token = token
        _cookies = cookies
    }

    override fun initClass(
        context: Context, deviceId: String,
        token: String?,
        cookies: HashSet<String>?
    ) {
        baseOkClient = OkHttpClientHelper.getInstance().getOKHttpClient(
            GetInterceptor(),
            TokenInterceptor(_deviceId, _token, _cookies)
        )
    }

    override fun <T> create(service: Class<T>, okHttpClient: OkHttpClient): T {
        return create<T>(service, okHttpClient, getBaseUrl())
    }

    override fun <T> create(service: Class<T>, okHttpClient: OkHttpClient, url: String): T {
        var baseUrl = url
        val builder = GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss")
        //gson过滤掉realm字段
        val gson = builder.setExclusionStrategies(object : ExclusionStrategy {
            override fun shouldSkipField(f: FieldAttributes): Boolean {
                return false
            }

            override fun shouldSkipClass(clazz: Class<*>?): Boolean {
                return false
            }
        }).create()

        if (baseUrl.isEmpty()) {
            baseUrl = getBaseUrl()
        }

        val retrofit = Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJava3CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .build()
        return retrofit.create(service)
    }

    override fun getHttpClient(
        level: HttpLoggingInterceptor.Level,
        vararg interceptors: Interceptor?
    ): OkHttpClient {
        val newBuilder: OkHttpClient.Builder = baseOkClient.newBuilder()
        setHttpLoggingInterceptor(newBuilder, level)
        for (interceptor in interceptors) {
            newBuilder.addInterceptor(interceptor!!)
        }
        return newBuilder.build()
    }

    override fun getHttpClient(
        readTimeOut: Int,
        writeTimeOut: Int,
        connectTimeOut: Int,
        level: HttpLoggingInterceptor.Level,
        vararg interceptors: Interceptor?
    ): OkHttpClient {
        val newBuilder: OkHttpClient.Builder = baseOkClient.newBuilder()
        setHttpLoggingInterceptor(newBuilder, level)
        newBuilder.connectTimeout(connectTimeOut.toLong(), TimeUnit.SECONDS)
        newBuilder.readTimeout(readTimeOut.toLong(), TimeUnit.SECONDS)
        newBuilder.writeTimeout(writeTimeOut.toLong(), TimeUnit.SECONDS)
        for (interceptor in interceptors) {
            newBuilder.addInterceptor(interceptor!!)
        }
        return newBuilder.build()
    }

    private fun setHttpLoggingInterceptor(
        newBuilder: OkHttpClient.Builder,
        level: HttpLoggingInterceptor.Level
    ) {
        //if (BuildConfig.DEBUG) {
        val logging = HttpLoggingInterceptor()
        logging.setLevel(level)
        newBuilder.addInterceptor(logging)
        //}
    }

    private fun getBaseUrl(): String {
        val baseUrl = "http://m-dev.dzj.com/doabf/"
        return "${baseUrl}."
    }
}